<?php
/**
 * 失效wiki推荐列表
 */

use Biz\model\ShopCategoryClassify;
use Biz\module\Classes\wiki\Infos as clsInfos;
use Biz\pockets\constInterface\WikiFields;
use Biz\service\wiki\FindWIkiService;
use Biz\service\wiki\Wiki;
use Biz\tools\helper\Common;
use Doraemon\tools\Log;


class ExpireWikiRecListAction extends ActionPlugin
{
    public const TAB_TYPE = 'expire_wiki_rec_list';
    public const CONFIG_FILE = 'recommendation_api';
    public const CONFIG_SELECT = 'wiki';
    const USER_LEVEL = self::LEVEL_NOTHING;
    const IS_LOG_EXPOSE = 1;

    /**
     * 失效wiki推荐列表
     * @return void
     * @throws Exception
     */
    public function main()
    {
        $wiki_id = $this->getRequest()->getPost('wiki_id', 0);
        $act_from = $this->getRequest()->getPost('act_from', 'expire_wiki_detail'); //来源标识
        $page = $this->getRequest()->getPost('page', 1); //来源标识
        $per_page = $this->getRequest()->getPost('per_page', 6); //来源标识
        $uid = $this->uid;

        $list['list'] = [];
        $list['total'] = 0;
        $list['per_page'] = $per_page;
        $list['current_page'] = $page;
        $list['is_over'] = 1;
        if (!$wiki_id) {
            return $this->setSuccess($list);
        }


        $url = Common::getUrl(self::TAB_TYPE, self::CONFIG_FILE, self::CONFIG_SELECT);

        $params = [
            'wiki_id' => $wiki_id,
            'rec_length' => $per_page,
            'page' => $page
        ];
        //算法获取wiki_id 合集
        $response_info = Common::requestUrl('get', $params, $url);
        $mes = [
            'params' => $params,
            'url' => $url,
            'response' => $response_info
        ];
        Log::debug('wiki失效推荐列表', $mes);

        if (!$response_info) {
            $default_info = $this->defaultWikiList($wiki_id, $uid, $page, $per_page);
            $wiki_ids = $default_info['wiki_ids'] ?: 0;
            $count = $default_info['count'] ?: 0;
        } else {
            $wiki_ids = $response_info;
            $count = $per_page;
        }

        if (!$wiki_ids) {
            return $this->setSuccess($list);
        }
        $wiki_list = clsInfos::infos($wiki_ids, WikiFields::FIELDS_GROUP_MIDDLE_LIST, $uid);
        if (!$wiki_list) {
            return $this->setSuccess($list);
        }
        $current = $page * $per_page;
        $list['total'] = $count ?: 0;
        $list['per_page'] = $per_page;
        $list['current_page'] = $page;
        $list['is_over'] = ($count <= $current) ? 1 : 0;
        $list['list'] = array_values($wiki_list);
        $this->setSuccess($list);
    }

    /**
     * 兜底方案
     */
    private function defaultWikiList($wiki_id, $uid, $page, $per_page)
    {
        //查询出当前wiki分类信息
        $wiki_info = Wiki::getWikiBaseInfos([$wiki_id]);
        if (!$wiki_info) {
            return [];
        }
        $wiki_info = $wiki_info[$wiki_id] ?? [];
        $category_id = $wiki_info['category_id'] ?? 0;
        $second_category_id = $wiki_info['second_category_id'] ?? 0;
        $third_category_id = $wiki_info['third_category_id'] ?? 0;
        $four_category_id = $wiki_info['four_category_id'] ?? 0;

        //分类数组
        $cate_ids = [
            $category_id,
            $second_category_id,
            $third_category_id,
            $four_category_id,
        ];
        //分类信息
        $cate_ids = array_filter($cate_ids);

        if (!$cate_ids) {
            return [];
        }
        //查询父级分类
        $where['cate_id'] = $cate_ids;
        $parent_ids = ShopCategoryClassify::selectInfos($where, 'parent_id', false);
        Log::debug("查询父级分类", $parent_ids);
        if (!$parent_ids) {
            return [];
        }

        //二级分类
        $info = ShopCategoryClassify::selectInfos(['id' => $parent_ids, 'deep' => 2],
            ['id', 'classify_name', 'cate_id'], true);
        Log::debug("查询二级分类", $info);
        if (!$info) {
            return [];
        }
        //随机获取一个二级分类
        $array_key = array_rand($info, 1);
        $cate_info = $info[$array_key];
        if (!$cate_info) {
            return [];
        }
        $classify_id = $cate_info['id'];
        Log::debug("随机获取一个二级分类", $classify_id);
//            $data['statSign'] = basicStatSign::genStatSign(basicStatSign::MODULE_FIND_WIKI_FIND_WIKI_COLLECTION, [],
//                [
//                    'uid' => $this->uid,
//                    'act_from' => $act_from,
//                ]);
        $data['count'] = FindWIkiService::getCategoryWikiIdCount($classify_id, $wiki_id) ?: 0;
        $data['wiki_ids'] = FindWIkiService::getWikiIdsByClassifyId($classify_id, $page, $per_page, $wiki_id) ?: [];
        return $data;
    }

}